package com.test.dao.impl;

import com.test.dao.AddressDao;
import com.test.domain.Address;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class AddressDaoImpl implements AddressDao {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public void insertAddress(Address address) {
        String temp = address.getCustomerPhoneNumber();
        String addressNumber= "ad"+System.currentTimeMillis() +temp.substring(temp.length() - 7);
        System.out.println(addressNumber);
        String sql="insert into address values(?,?,?,?,?)";
        jdbcTemplate.update(sql,addressNumber,address.getName(),address.getPhoneNumber(),
                address.getAddress(),address.getCustomerPhoneNumber());
    }

    @Override
    public List<Address> getAddressListByPhoneNumber(String customerPhoneNumber) {
        String sql = "select * from address where customer_phone_number=?";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Address>(Address.class),customerPhoneNumber);
    }

    @Override
    public void updateNameByAddressNumber(String addressNumber, String name) {
        String sql = "update address set name=? where address_number=?";
        jdbcTemplate.update(sql,name,addressNumber);
    }

    @Override
    public void updatePhoneNumberByAddressNumber(String addressNumber, String phoneNumber) {
        String sql = "update address set phone_number=? where address_number=?";
        jdbcTemplate.update(sql,phoneNumber,addressNumber);
    }

    @Override
    public void updateAddressByAddressNumber(String addressNumber, String address) {
        String sql = "update address set address=? where address_number=?";
        jdbcTemplate.update(sql,address,addressNumber);
    }

    @Override
    public void deleteAddressByAddressNumber(String addressNumber) {
        String sql = "delete address where address_number=?";
        jdbcTemplate.update(sql,addressNumber);
    }
}
